<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <button id="save">保存</button>
    <button id="get">读取</button>
    <button id="del">删除</button>
    <button id="del">清空</button>

    <script>


        const user={
             name:'牛哥',
             age:18
        }
        document.getElementById('save').addEventListener('click',()=>{
            localStorage.setItem('user',JSON.stringify(user)) //前面是一个key，可以随便命名
             //读取数据
        })
        document.getElementById('get').addEventListener('click', () => {
                localStorage.setItem('user', JSON.parse(user))     
            })
         document.getElementById('del').addEventListener('click', () => {
                localStorage.removeItem('user')
            })
         document.getElementById('del').addEventListener('click', () => {
                localStorage.clear()   //移除数据
            })


     //
</script>
</body>
</html>